// pages/order/creatOrder/map/selectMap.js
import getIn from '../../../../sdk/utils/getIn';
import QQMapWX from '../../../../sdk/lib/qqmap-wx-jssdk.min.js';
import { notify } from "../../../../sdk/utils/NotifyRegistryCenter.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [{
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
    }],
    ad_info: {
      address: '',
      recommend: '',
    },
    default_info:{
      address: '',
      recommend: '',
    },
    addressVal: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.mapCtx = wx.createMapContext('myMap');
    this.getLocation()
    // 兼容cover-view 1.9.90版本一下不能用overflow: scroll
    wx.getSystemInfo({
      success: (res) => {
        const version = res.SDKVersion.replace(/\./g, '');
        this.setData({ version: parseInt(version, 10)});
      }
    })
  },
  // 中文搜索地址
  onChange(e) {
    const value = e.detail.value;
    const qqmapsdk = new QQMapWX({
      key: 'PMFBZ-RANLG-C4OQT-IKVXS-PO3FQ-D5FEO' // 必填
    });
    qqmapsdk.getSuggestion({
      keyword: value,
      policy: 1,
      success: (res) => {
        this.setData({ searchList: res.data });
      },
      fail: (res) => {
        showFail && showFail('搜索地址失败')
      },
    });
  },
  // 监听地图改变后
  regionchange(e) {
    if (e.type == 'end') {
      this.mapCtx.getCenterLocation({
        success: (res) => {
          const location = {
            latitude: res.latitude,
            longitude: res.longitude,
          }
          if (!(res.latitude == this.data.latitude && res.longitude == this.data.longitude)) {
            this.getLngLat(location);
          }
        }
      })
    }
  },
  // 初始化当前地址
  getLocation() {
    const address = JSON.parse(this.options.address);
    if (address.lat && address.lng) {
      const location = {
        latitude: address.lat,
        longitude: address.lng,
      }
      this.setData({ addressVal: address.addressVal })
      this.getLngLat(location)
    } else {
      wx.getLocation({
        type: 'gcj02',
        success: (res) => {
          const location = {
            latitude: res.latitude,
            longitude: res.longitude,
          }
          this.getLngLat(location)
        }
      });
    }
    
  },
  // 腾讯逆地址解释
  getLngLat(location) {
    const qqmapsdk = new QQMapWX({
      key: 'PMFBZ-RANLG-C4OQT-IKVXS-PO3FQ-D5FEO' // 必填
    });
    qqmapsdk.reverseGeocoder({
      location,
      coord_type: 5,
      get_poi: 1,
      success: (addressRes) => {
        const result = addressRes.result;
        const ad_info = {
          address: result.address,
          recommend: result.formatted_addresses.recommend,
        }
        this.setData({
          latitude: location.latitude,
          longitude: location.longitude,
          markers: [{
            id: 0,
            latitude: location.latitude,
            longitude: location.longitude,
          }],
          ad_info
        });
      }
    });
  },
  // 重新定位当前位置
  moveToLocation() {
    this.mapCtx.moveToLocation()
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const location = {
          latitude: res.latitude,
          longitude: res.longitude,
        }
        this.getLngLat(location)
      }
    });
  },
  // 清空当前搜索列表
  searchListHidden() {
    this.setData({ searchList: [] });
  },
  // 选择地点
  onSelect(e) {
    const item = getIn(e.currentTarget.dataset, ['item']);
    this.setData({
      latitude: getIn(item.location, ['lat']),
      longitude: getIn(item.location, ['lng']),
      markers: [{
        id: 0,
        latitude: getIn(item.location, ['lat']),
        longitude: getIn(item.location, ['lng']),
      }],
      ad_info: {
        address: item.address,
        recommend: item.title,
      },
      searchList: []
    });
  },
  // 详细地址
  changeAddress(e) {
    const value = e.detail.value;
    this.setData({ addressVal: value });
  },
  // 提交
  submit(){
    const params = {
      key: this.options.type,
      address: `${this.data.ad_info.address}${this.data.addressVal}`,
      lat: this.data.latitude,
      lng: this.data.longitude,
      addressVal: this.data.addressVal
    }
    notify && notify('updateAddress', params);
    global.navigateHelper.navigateBack()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})